<% # frozen_string_literal: true %>

<div class="modal fade user-assignments-modal <%= assignable.class.name.parameterize(separator: '-') %>-assignments-modal" tabindex="-1" role="dialog" data-action="modal-close">
  <div class="modal-dialog" role="document">
    <div class="modal-content" id="user_assignments_modal">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><i class="sn-icon sn-icon-close"></i></button>
        <h4 class="modal-title"><%= t "access_permissions.#{assignable.class.name.pluralize.underscore}.modals.edit_modal.title", resource_name: assignable.name %></h4>
      </div>
      <div class="modal-body">
        <% manually_assigned_users.order(full_name: :asc).each do |user| %>
          <%
            options = { user: user, with_inherit: !assignable.top_level_assignable?, assignable: assignable, update_path: public_send("access_permissions_#{assignable.class.name.underscore}_path", assignable) }
            options.merge!(delete_path: public_send("access_permissions_#{assignable.class.name.underscore}_path", assignable, user_id: user.id)) if assignable == top_level_assignable
          %>

          <%= render('access_permissions/partials/member_field', options) %>
        <% end %>
        <%= render('access_permissions/partials/default_public_user_role_form', assignable: top_level_assignable, editable: assignable == top_level_assignable) if top_level_assignable.respond_to?(:visible?) && top_level_assignable.visible? %>
      </div>

      <% if assignable.top_level_assignable? %>
        <div class="modal-footer">
          <%= link_to new_assignment_path, class: 'btn btn-secondary pull-right', data: { action: 'swap-remote-container', target: '#user_assignments_modal' } do %>
            <i class="sn-icon sn-icon-new-task"></i>
            <%= t('access_permissions.grant_access') %>
          <% end %>
        </div>
      <% end %>
    </div>
  </div>
</div>
